package com.example.jl.bethinner.activity;

import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

import com.example.jl.bethinner.R;
import com.example.jl.bethinner.base.BaseActivity;

import java.util.regex.Pattern;

public class TestIncludeNumberActivity extends BaseActivity {
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_include_number);
        textView = (TextView) findViewById(R.id.textView);
        final TextInputLayout inputLayout = (TextInputLayout) findViewById(R.id.textInput);
        inputLayout.setHint("请输入测试字段:");

        EditText editText = inputLayout.getEditText();
        editText.setHintTextColor(getResources().getColor(android.R.color.holo_blue_bright));
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 10) {
                    inputLayout.setErrorEnabled(true);
                    inputLayout.setError("测试字段超过10个了");
                } else {
                    inputLayout.setErrorEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (hasDigit(s.toString())) {
                    if (isNumeric(s.toString())) {
                        textView.setText("纯数字");
                    } else {
                        textView.setText("含有数字");
                    }
                } else {
                    textView.setText("不含有数字");
                }

            }


        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_test_include_number, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    // 判断一个字符串是否都为数字  用正则表达式
    public static boolean isNumeric(String str) {

        Pattern pattern = Pattern.compile("[0-9]*");

        return pattern.matcher(str).matches();

    }
    // 判断一个字符串是否含有数字

    public boolean hasDigit(String content) {
        return Pattern.compile(".*\\d+.*").matcher(content).matches();

    }
}
